package com.google.gwt.it2tool.client;

import java.io.Serializable;

public class Production implements Serializable{
	
	private final static int STEP = 5;
	
	private String id;
	private String name;
	private double area;
	private double productivity;
	private double duration;
	
	private final ClientDataCache clientDataCache;
	
	public Production(){
		super();
		clientDataCache = ClientDataCache.getInstance();
	}

	public Production(String id, String name, double area, double productivity, double duration) {
		super();
		this.id=id;
		this.name = name;
		this.area = area;
		this.duration = duration;
		this.productivity = productivity;
		clientDataCache = ClientDataCache.getInstance();
	}

	public Production(String name) {
		super();
		this.name = name;
		clientDataCache = ClientDataCache.getInstance();
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public double getArea() {
		return area;
	}

	public void setArea(double area) {
		this.area = area;
	}
	
	public double getDuration() {
		return duration;
	}

	public void setDuration(int duration) {
		this.duration = duration;
	}

	public double getProductivity() {
		return productivity;
	}

	public void setProductivity(int productivity) {
		this.productivity = productivity;
	}
	
	public String getId(){		
		return id;
	}
	
	public void setId(String id){
		this.id=id;
	}
	
	public void generateId(){
		id = IdGenerator.getNextId();
	}
	
	public int getOutput(){	
		if (area<=0){
			return 0;
		}
		double mins = getMins();

		double cycles = 1440d/mins;
		cycles = Math.floor(cycles);
		return (int) ((int)cycles*productivity);
	}
	
	public int getMins(){
		return getMins(area);
	}
	
	public int getMins(double area){
		if (area<=0){
			return 0;
		}
		double bonus = ((clientDataCache.getMassProdLevel()*2.5d)/100d)*area;
		return (int) Math.ceil(duration/(((area + bonus))/100d)*60d);
	}
	
	public int getMinUpArea(){
		int mins =  getMins();
		
		if (mins==0) return 0;
		
		mins--;
		
		//Rounding the Area to be in 50 steps always regardless what the area value is exactly
		int a = (int) ((Math.round(area / STEP) * STEP)+STEP);
		while (a>=0 && a<=10000 && (getMins(a)>mins)){
			a=a+STEP;
		}

		if (a<0) return 0;
		if (a>10000) return 10000;
		return a;		
	}
	
	public int getMinDownArea(){
		int mins =  getMins();
		
		if (mins==0) return 0;
		
		mins++;
		//Rounding the Area to be in 50 steps always regardless what the area value is exactly
		int a = (int) ((Math.round(area / STEP) * STEP)-STEP);
		while (a>=0 && a<=10000 && getMins(a)<mins){
			a=a-STEP;
		}
		if (a<0) return 0;
		if (a>10000) return 10000;
		return a;			
	}

}
